package com.cx.thread.pool;

import com.sun.corba.se.spi.orbutil.threadpool.ThreadPool;

import java.util.concurrent.*;

/**
 * @author zhangshuai
 * @version V1.0
 * @Project Java2_26
 * @Package com.cx.thread.pool
 * @Description:
 * @Date 2025/5/26 17:04
 */

public class ThreadPoolTest {
    public static void main(String[] args) {
       //使用接口实现类 ThreadPoolExcutor
        ExecutorService executor = new ThreadPoolExecutor(2, //核心线程数
                5,//最大线程数
                1, //超过空闲时间会被回收
                TimeUnit.MINUTES,//空闲时间单位
                new ArrayBlockingQueue<>(3),//工作队列
                Executors.defaultThreadFactory(),
                 //线程工厂，创建线程
                new ThreadPoolExecutor.DiscardOldestPolicy()//拒绝策略
        );
        executor.execute(()->{
            for (int i = 0; i <10 ; i++) {
                System.out.println(Thread.currentThread().getName()+"线程" + i);
            }

        });
        /*int n = 5;
        for (int i = 0; i < n; i++) {
            Runnable run = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(10);
                        System.out.println("执行当前任务的线程：" + Thread.currentThread().getName());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };*/
            //执行任务
            /*executor.execute(run);



            //工具类 Excutors
            ExecutorService executorService = Executors.newFixedThreadPool(5);
            ExecutorService executorService1 = Executors.newSingleThreadExecutor();
            ExecutorService executorService2 = Executors.newCachedThreadPool();
            ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
*/
        }

}